package com.webratio.units.custom.socialgmapunit;
import java.util.Collections;
import java.util.Map;
import org.dom4j.Element;
import com.webratio.rtx.RTXContentUnitService;
import com.webratio.rtx.RTXException;
import com.webratio.rtx.RTXManager;
import com.webratio.rtx.core.AbstractService;
import com.webratio.rtx.core.DescriptorHelper;

public class SocialGMapUnitService extends AbstractService implements RTXContentUnitService{

	// Pre-defined Values
	private int zoom;
	private int width;
	private int height;
	private double longitude;
	private double latitude;
	private String mapType;

	public SocialGMapUnitService(String id, RTXManager mgr, Element descr) throws RTXException {
		super(id, mgr, descr);
		// TODO Auto-generated constructor stub

		// Selection of the Unit in this case...
		Element currentUnit = descr.element("SocialGMapUnit");

		// Default Value
		width = 800;
		height = 400;
		latitude = 34;
		longitude = 0;
		zoom = 1;

		//Extracted from XML file in other words since the WebML Interface
		try {
			zoom = Integer.parseInt(DescriptorHelper.getAttribute(currentUnit,
					"zoom", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			width = Integer.parseInt(DescriptorHelper.getAttribute(currentUnit,
					"width", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			height = Integer.parseInt(DescriptorHelper.getAttribute(
					currentUnit, "height", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			latitude = Double.parseDouble(DescriptorHelper.getAttribute(
					currentUnit, "latitude", true, this));
		} catch (NumberFormatException ex) {
		}
		try {
			longitude = Double.parseDouble(DescriptorHelper.getAttribute(
					currentUnit, "longitude", true, this));
		} catch (NumberFormatException ex) {
		}
		mapType = DescriptorHelper.getAttribute(currentUnit, "mapType", true,
				this);

	}

	public Object computeParameterValue(String outputParamName, Map pageContext, Map sessionContext) throws RTXException {
		// TODO Auto-generated method stub
		return null;
	}

	public Object execute(Map pageContext, Map sessionContext) throws RTXException {
		// TODO Auto-generated method stub
		// bean inizialization
		SocialGMapUnitBean unitBean = getUnitBean(pageContext, sessionContext);
		if (unitBean == null) {
			return null;
		}

		return unitBean;
	}

	private SocialGMapUnitBean getUnitBean(Map pageContext, Map sessionContext) throws RTXException {
		// TODO Auto-generated method stub
		SocialGMapUnitBean unitBean = (SocialGMapUnitBean) pageContext
		.get('_' + getId());
		if (unitBean == null || unitBean.getDataSize() == 0) {
			unitBean = createUnitBean(pageContext, sessionContext);
		}
		// Insert values into the bean
		unitBean.setWidth(width);
		unitBean.setHeight(height);
		unitBean.setLatitude(latitude);
		unitBean.setLongitude(longitude);
		unitBean.setZoom(zoom);
		unitBean.setMapType(mapType);

		pageContext.put('_' + getId(), unitBean);

		return unitBean;
	}

	private SocialGMapUnitBean createUnitBean(Map pageContext,
			Map sessionContext) {
		// TODO Auto-generated method stub
		SocialGMapUnitBean bean = new SocialGMapUnitBean();

		bean.setDataSize(0);
		bean.setData(Collections.EMPTY_LIST);

		return bean;
	}

	public void dispose() {
		// TODO Auto-generated method stub
	}

}